import { Component, OnInit } from '@angular/core';
import {NavController} from "@ionic/angular";

@Component({
  selector: 'app-d09',
  templateUrl: './d09.page.html',
  styleUrls: ['./d09.page.scss'],
})
export class D09Page implements OnInit {

  constructor(public navCtrl:NavController) { }

  ngOnInit() {
  }
  click1(){
    let map = new BMap.Map("bdmap");
    map.centerAndZoom(new BMap.Point(116.404,39.915),15);
    let myP1 = new BMap.Point(116.380967,39.913285);
    let myP2 = new BMap.Point(116.424374,39.914668);
    let myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/Mario.png",new BMap.Size(32,70),{
      anchor: new BMap.Size(0,-5),
      imageOffset: new BMap.Size(0,0)
    });
    let driving2 = new BMap.DrivingRoute(map,{renderOptions:{map:map,autoViewport:true}});
    driving2.search(myP1,myP2);
    let run =()=>{
      let driving = new BMap.DrivingRoute(map);
      driving.search(myP1,myP2);
      driving.setSearchCompleteCallback(()=>{
        let pts = driving.getResults().getPlan(0).getRoute(0).getPath();
        let paths = pts.length;
        let carMk = new BMap.Marker(pts[0],{icon:myIcon});
        map.addOverlay(carMk);
        let resetMkPoint = (i:number) =>{
          carMk.setPosition(pts[i]);
          if (i < paths){
            setTimeout(()=>{
              i++;
              resetMkPoint(i);
            },100);
          }
        };
        let i = 0;
        setTimeout( async ()=>{
          if(i<6){
            resetMkPoint(i);
            i++;
          }

        },100);

      });
    };
    setTimeout(async ()=>{await run();},1500);
  }
  click2(){
    let map = new BMap.Map("bdmap");
    map.centerAndZoom(new BMap.Point(116.404,39.915),15);
    let bounds = null;
    let linesPoints = null;
    let spoi1 = new BMap.Point(116.380967,39.913285);
    let spoi2 = new BMap.Point(116.380967,39.953285);
    let epoi = new BMap.Point(116.424374,39.914668);
    let myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/Mario.png",new BMap.Size(32,70),{imageOffset:new BMap.Size(0,0)});
    let initLine = () =>{
      bounds = new Array();
      linesPoints = new Array();
      map.clearOverlays();
      let driving3 = new BMap.DrivingRoute(map,{onSearchComplete:drawLine});
      driving3.search(spoi1,epoi);
      let driving4 = new BMap.DrivingRoute(map,{onSearchComplete:drawLine});
      driving4.search(spoi2,epoi);
    };
    let run = ()=>{
      let resetMkPoint = (i,len,pts,carMk)=>{
        carMk.setPosition(pts[i]);
        if(i <len){
          setTimeout( async ()=>{
            i++;
            resetMkPoint(i,len,pts,carMk);
          },100);
        }
      };
      for (let m = 0;m<linesPoints.length;m++){
        let pts = linesPoints[m];
        let len = pts.length;
        let carMk = new BMap.Marker(pts[0],{icon: myIcon});
        map.addOverlay(carMk);
        resetMkPoint(1,len,pts,carMk)
      }
    };
    let drawLine = (results) => {
      let opacity = 0.45;
      let planObj = results.getPlan(0);
      let b = new Array();
      let addMarkerFun = (point,imgType,index,title?)=>{
        let url;
        let width;
        let height;
        let myIcon;
        if(imgType == 1){
          url = "http://lbsyun.baidu.com/jsdemo/img/dest_markers.png";
          width = 42;
          height = 34;
          myIcon = new BMap.Icon(url,new BMap.Size(width,height),{anchor:new BMap.Size(14,32),imageOffset:new BMap.Size(0,0-index*height)});
        }else{
          url = "http://lbsyun.baidu.com/jsdemo/img/trans_icons.png";
          width = 22;
          height = 25;
          let d = 25;
          let cha = 0;
          let jia = 0;
          if(index == 2){
            d = 21;
            cha = 5;
            jia = 1;
          }
          myIcon = new BMap.Icon(url,new BMap.Size(width,d),{
            anchor: new BMap.Size(10,(11+jia)),
            imageOffset:new BMap.Size(0,0-index*height-cha)
          });
        }
        let marker = new BMap.Marker(point,{icon:myIcon});
        if(title != null && title != ""){
          marker.setTitle(title);
        }
        if(imgType == 1){
          marker.setTop(true);
        }
        map.addOverlay(marker);
      };
      let addPoints = (points) => {
        for(let i = 0;i < points.length;i++){
          bounds.push(points[i]);
          b.push(points[i]);
        }
      };

      for (let i = 0;i < planObj.getNumRoutes();i++){
        let route = planObj.getRoute(i);
        if(route.getDistance(false)<=0){continue;}
        addPoints(route.getPath());
        if(route.getRouteType()==BMap.RouteType.BMAP_ROUTE_TYPE_DRIVING){
          map.addOverlay(new BMap.Polyline(route.getPath(),{strokeColor:"#0030ff",strokeOpacity:opacity,strokeWeight:6,enableMassClear:true}));
        }
      }
      map.setViewport(bounds);
      addMarkerFun(results.getEnd().point,1,1);
      addMarkerFun(results.getStart().point,1,0);
      linesPoints[linesPoints.length] = b;
    };
    initLine();
    setTimeout(async ()=>{
      await run();
    },1500);
  }
}
